<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" icon="el-icon-plus" @click="add"></el-button>
				<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<el-input v-model="search.keyword" placeholder="表单名称" clearable></el-input>
					<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :params="{model:'1'}" :apiObj="apiObj" row-key="formId" @selection-change="selectionChange" stripe remoteSort>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="表单名称" prop="formTitle" width="200" sortable='custom' :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="表单类型" prop="formType" width="100">
					<template #default="scope">
						<el-button type="danger" size="small" round v-if="scope.row.formType==='1'">内置</el-button>
						<el-button type="success" size="small" round v-else-if="scope.row.formType==='0'">自建</el-button>
					</template>
				</el-table-column>
				<el-table-column label="数据库表" prop="tableName" width="150" sortable='custom'>
					<template #default="scope">
						b_{{scope.row.tableName}}
					</template>
				</el-table-column>
				<el-table-column label="日志状态" prop="dataLogFlag" width="100">
					<template #default="scope">
						<el-button type="success" round v-if="scope.row.dataLogFlag==='1'" icon="el-icon-check" circle></el-button>
						<el-button type="info" round v-if="scope.row.dataLogFlag==='0'" icon="el-icon-close" circle></el-button>
					</template>
				</el-table-column>
				<el-table-column label="创建人" prop="userName" width="100"></el-table-column>
				<el-table-column label="创建时间" prop="createTime" width="150"></el-table-column>
				<el-table-column label="版本标识" prop="version" width="300"></el-table-column>
				<el-table-column label="备注" prop="remark" width="100" :show-overflow-tooltip="true"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="260">
					<template #default="scope">
						<el-button-group>
							<el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)">编辑</el-button>
							<el-button text type="primary" size="small" @click="designer(scope.row, scope.$index)">设计</el-button>
							<el-button text type="primary" size="small" @click="preview(scope.row, scope.$index)">预览</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="scope.row.formType!=='1'">
								<template #reference>
									<el-button text type="primary" size="small">删除</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
						<el-dropdown>
							<el-button icon="el-icon-more" size="small"></el-button>
							<template #dropdown>
								<el-dropdown-menu>
									<el-dropdown-item @click="dataRecordView(scope.row,scope.$index)">记录</el-dropdown-item>
									<el-dropdown-item @click="dataFieldView(scope.row,scope.$index)">字段</el-dropdown-item>
									<el-dropdown-item @click="formVersionView(scope.row,scope.$index)">版本</el-dropdown-item>
									<el-dropdown-item @click="exportFormTemplate(scope.row,scope.$index)">导出</el-dropdown-item>
									<el-dropdown-item @click="printTemplateView(scope.row,scope.$index)">模版</el-dropdown-item>
								</el-dropdown-menu>
							</template>
						</el-dropdown>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>

	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>

</template>

<script>
import saveDialog from './save'

export default {
	name: 'Document from',
	components: {
		saveDialog,
	},
	data() {
		return {
			dialog: {
				save: false
			},
			search: {
				keyword: null
			},
			apiObj: this.$API.bpm.bpmForm.getBpmFormListForManage,
			selection: []
		}
	},
	mounted() {

	},
	methods: {
		designer(row, index)
		{
			this.$nextTick(() => {
				window.open('/#/doc/setting/bpmform/designer?formId='+row.formId+"&ddtab=true", "_blank");
			})
		},
		preview(row,index)
		{
			this.$nextTick(() => {
				window.open('/#/doc/setting/bpmform/preview?formId='+row.formId+"&ddtab=true", "_blank");
			})
		},
		dataRecordView(row, index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/doc/setting/bpmform/datarecord',
					query:{
						formId:row.formId,
						dataLogFlag:row.dataLogFlag,
						formTitle:row.formTitle,
					}
				})
			})
		},
		dataFieldView(row, index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/doc/setting/bpmform/datafield',
					query:{
						formId:row.formId
					}
				})
			})
		},
		formVersionView(row, index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/doc/setting/bpmform/formversion',
					query:{
						formId:row.formId
					}
				})
			})
		},
		printTemplateView(row, index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/doc/setting/bpmform/printtemplate',
					query:{
						formId:row.formId
					}
				})
			})
		},
		exportFormTemplate(row,index)
		{
			this.$API.bpm.bpmForm.getBpmFormTemplateToFile.fileDownload({formId:row.formId});
		},
		//增加
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open()
			})
		},
		//编辑
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//删除
		async table_del(row, index){
			var res = await this.$API.bpm.bpmForm.delete.post({formId: row.formId});
			if(res.code == 200){
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del(){
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				const loading = this.$loading();
				var idsArr=[];
				this.selection.forEach(item => {
					idsArr.push(item.formId)
				})
				var res = await this.$API.bpm.bpmForm.deleteBpmFormByIds.post({ids: idsArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
				this.$message.success("操作成功")
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		//本地更新数据
		handleSuccess(){
			this.$refs.table.refresh()
		},
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
	}
}
</script>

<style>
</style>
